package com.pur.zsms.accountmanage.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.pur.zsms.accountmanage.dao.IAccountDao;
import com.pur.zsms.accountmanage.po.StoreInAccountDetailedPo;
import com.pur.zsms.accountmanage.po.StoreInAccountPo;
import com.pur.zsms.accountmanage.service.IAccountService;
import com.pur.zsms.accountmanage.vo.AccountDayDetailVo;
import com.pur.zsms.accountmanage.vo.AccountDayVo;
import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.accountmanage.vo.StoreInAccountDetailedVo;
import com.pur.zsms.accountmanage.vo.StoreInAccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.storemanage.dao.IStoreDao;
import com.pur.zsms.storemanage.po.StoreInDetailedPo;
import com.pur.zsms.storemanage.po.StoreInPo;
import com.pur.zsms.storemanage.po.StoreMaterPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedPo;
import com.pur.zsms.storemanage.po.StoreOutPo;
import com.pur.zsms.storemanage.po.StorePo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInDetailedVo;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutDetailedVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.storemanage.vo.StoreVo;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {

	@Resource(name = "storeDao")
	private IStoreDao storeDao;

	@Resource(name = "accountDao")
	private IAccountDao accountDao;

	public IStoreDao getStoreDao() {
		return storeDao;
	}

	public void setStoreDao(IStoreDao storeDao) {
		this.storeDao = storeDao;
	}

	public IAccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(IAccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Override
	public PaginationSupport listAccountsDetialed(PageController pageController, AccountVo accountVo)
	        throws GenericException {
		return accountDao.listAccountDetailed(pageController, accountVo);
	}

	@Override
	public PaginationSupport listAccountsDetialedSelect(PageController pageController, AccountVo accountVo)
	        throws GenericException {
		return accountDao.listAccountDetailedSelect(pageController, accountVo);
	}

	@Override
	public PaginationSupport listAccountProcess(PageController pageController,
	        StoreInAccountVo storeInAccountVo) throws GenericException {
		return accountDao.listAccountProcess(pageController, storeInAccountVo);
	}

	/**
	 * 入库结算
	 * */
	public void commitAccount(StoreInAccountDetailedVo storeInAccountDetailedVo) throws GenericException {
		// 处理前台传入json数据 --页脚合计
		JSONArray arrayFoot = JSONArray.fromObject(storeInAccountDetailedVo.getFootList());

		BigDecimal materValueAll = new BigDecimal(0);
		for (int i = 0; i < arrayFoot.size(); i++) {
			JSONObject object = JSONObject.fromObject(arrayFoot.get(i));
			materValueAll = new BigDecimal(object.get("materValue").toString());
		}
		// 插入主表
		StoreInAccountPo tStoreInAccountPo = new StoreInAccountPo();
		tStoreInAccountPo.setAccountDate(PubFun.getCurrentDate());
		tStoreInAccountPo.setMaterValueAll(materValueAll);
		tStoreInAccountPo.setRemark(storeInAccountDetailedVo.getRemark());
		int accountId = accountDao.addStoreInAccountPo(tStoreInAccountPo);

		// 处理前台传入json数据 --关联id
		JSONArray array = JSONArray.fromObject(storeInAccountDetailedVo.getIdList());
		for (int i = 0; i < array.size(); i++) {
			JSONObject object = JSONObject.fromObject(array.get(i));

			StoreInAccountDetailedPo tStoreInAccountDetailedPo = new StoreInAccountDetailedPo();
			tStoreInAccountDetailedPo.setId(Integer.valueOf(object.get("id").toString()));
			tStoreInAccountDetailedPo.setAccountId(accountId);
			accountDao.addStoreInAccountDetailedPo(tStoreInAccountDetailedPo);

			StoreInDetailedPo tStoreInDetailedPo = storeDao.getStoreInDetailedPo(Integer.valueOf(object.get(
			        "id").toString()));
			tStoreInDetailedPo.setAccountState(2);
			storeDao.addStoreInDetailedPo(tStoreInDetailedPo);

		}

	}

	@Override
	public PaginationSupport accountListForCust(PageController pageController, AccountDayVo accountDayVo)
	        throws GenericException {
		return accountDao.accountListForCust(pageController, accountDayVo);
	}

	@Override
	public PaginationSupport accountListForCustMonth(PageController pageController, AccountDayVo accountDayVo)
	        throws GenericException {
		return accountDao.accountListForCustMonth(pageController, accountDayVo);
	}

	public PaginationSupport accountListForCustDetailForOrder(PageController pageController,
	        AccountDayDetailVo accountDayDeatilVo) throws GenericException {
		return accountDao.accountListForCustDetailForOrder(pageController, accountDayDeatilVo);
	}

	public PaginationSupport accountListForCustDetailForStoreOut(PageController pageController,
	        AccountDayDetailVo accountDayDeatilVo) throws GenericException {
		return accountDao.accountListForCustDetailForStoreOut(pageController, accountDayDeatilVo);
	}
	
	public PaginationSupport findCaiwuCheck(PageController pageController, OrderSupplierVo orderSupplierVo)
	        throws GenericException{
		return accountDao.findCaiwuCheck(pageController, orderSupplierVo);
	}
}
